package com.l.web.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * Created by 严秋旺 on 2014/12/30.
 */
public class EncodeFilter implements Filter {

    private String encode = "utf-8";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String encode = filterConfig.getInitParameter("encode");
        if (encode != null && "".equals(encode.trim())) {
            this.encode = encode;
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding(encode);
        servletResponse.setCharacterEncoding(encode);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }

}
